var nowDate = getDay(0);
var pandianid = GetQueryString('pandianid');

function ICalendarClick(){
//	onload()
}
$(document).ready(function(){
	if(sessionStorage.getItem("Creload")=='true'){
	     window.location.reload();
		 sessionStorage.setItem("Creload", false);
	}else{
		$('#name').text(sessionStorage.getItem("name"));
		$('#shopname').text(sessionStorage.getItem("shopname"));
		$('#saveButton').on('click',function(){
			savePandian();
		});
		addPage();
//		getPandianJianchaInfo();
		getPandianJianchaFlag();
	}
});

//获取盘点列表数据
function getPandianJianchaInfo(){
	showloading();
	var sendData = {};
	sendData.shopcode=$('#pandianShop').val();
	sendData.pandianDate=$('#pandianDate').val();
	$.ajax({
		url: "../pandianevery/getPandianJianchaInfo"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res)
			$('#pandiandaiban span').text("(0)");
			$('#bipanxiang span').text("(0)");
			try {
				
				if(res.success){
					var resdata = res.data;
					$('#pandiandaiban span').text('('+resdata.daibannum+')');
					$('#bipanxiang span').text('('+resdata.bipannum+')');
				}else{
					layer.alert(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
			
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
	
}
//获取盘点列表状态数据
function getPandianJianchaFlag(){
	showloading();
	var sendData = {};
	sendData.pandianid=pandianid;
	sendData.shopcode=sessionStorage.getItem("shopcode");
	$.ajax({
		url: "../pandianevery/getPandianJianchaFlag"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res)
			$('a[caidan]').attr('flag','0');
//			$('a[caidan]').removeClass('btn-warning');
//			$('a[caidan]').addClass('btn-info');
			$('a[caidan]').find('i').hide();
			try {
				
				if(res.success){
					var resdata = res.data;
//					for(var el in resdata){
//						
//					}
					$('a[caidan]').each(function(i,obj){
						var key = $(obj).attr('id')+"Flag";
						if(resdata[key] == '1'){
							$(obj).attr('flag','1');
//							$(obj).addClass('btn-warning');
//							$(obj).removeClass('btn-info');
							$(obj).find('i').show();
						}
					});
					$('#biaozhunjiancha').attr('biaozhunid',resdata.biaozhunid);
				}else{
					layer.alert(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
			
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
	
}
//提交盘点检查
function savePandian(){
	var checkFlag = true;
	$('a[caidan]').each(function(i,obj){
		if($(obj).attr('flag') == '0'){
			checkFlag = false;
			layer.msg($(obj).text()+'未完成')
			return false;
		}
	});
//	checkFlag = true;
//	alert(checkFlag)
//	checkFlag = false;
	if(checkFlag){
		showloading();
		var sendData = {};
		sendData.id=pandianid;
		sendData.querenFlag = 0;
		sendData.querenUser = sessionStorage.getItem("userId");
		sendData.shopcode = sessionStorage.getItem("shopcode");
		sendData.shopname = sessionStorage.getItem("shopname");
		sendData.level = $('input[name="level"]:checked').val();
		sendData.flag = 4;
		sendData.wanchengUser = sessionStorage.getItem("userId");
		sendData.wanchengUsername = sessionStorage.getItem("name");
		$.ajax({
			url: "../pandianevery/updatePandianJiancha"
			,type:'post'
//			,async: false
			,data:JSON.stringify(sendData)
			,contentType: "application/json; charset=utf-8"
			,dataType:'json'
			,success: function(res){
				console.log(res)
				try {
					
					if(res.success){
//						history.go(-1);
						window.location.href='toIndex';
					}else{
						layer.alert(res.message);
					}
				} catch (e) {
					console.log(e)
				} finally{
					hideloading();
				}
				
			}
			,error:function(XMLHttpRequest, textStatus, errorThrown) {
				 console.log(XMLHttpRequest);
				 console.log(textStatus);
				 console.log(errorThrown);
				 hideloading();
			}
		});
	}
	
}
//菜单点击页面跳转
function addPage(){
	$('#pandiandaiban').on('click',function(){
		window.location.href='toJiancha_daibanshixiang?pandianid='+pandianid;
	})
	$('#bipanxiang').on('click',function(){
		window.location.href='toJiancha_bipan?pandianid='+pandianid;
	})
	$('#zicaipandian').on('click',function(){
		window.location.href='toPandian_zicai_list?pandianid='+pandianid;
	})
	$('#changguipandian').on('click',function(){
		window.location.href='toJiancha_changguipandian?pandianid='+pandianid;
	})
	$('#biaozhunjiancha').on('click',function(){
		toBiaozhun();
		
	})
	$('#wentifankui').on('click',function(){
		window.location.href='toJiancha_wentifankui?pandianid='+pandianid;
	})
	$('input[name="level"]').on('click',function(){
		var obj = this;
		$('#levelDiv span').hide();
		$('#levelDiv span[level="'+$(obj).val()+'"]').show();
	});
}
//跳转标准盘点，如没有，先保存标准盘点
function toBiaozhun(){
	var biaozhunid = $('#biaozhunjiancha').attr('biaozhunid');
	var shopcode = sessionStorage.getItem("shopcode");
	var shopname = sessionStorage.getItem("shopname");
	var dudaoNO = sessionStorage.getItem("dudaoNO");
	var dudaoName = sessionStorage.getItem("dudaoName");
	var showFlag="show";
	if(biaozhunid == ''){
		var sendData = {};
		var jobnumber = sessionStorage.getItem("jobnumber");
		sendData.dudao = dudaoNO;
		sendData.sEmployeeNO = jobnumber;
		sendData.userId = sessionStorage.getItem("userId");
		sendData.shopcode = shopcode;
		sendData.answers = JSON.stringify({});
		sendData.score = 0;
		sendData.poweranswer = JSON.stringify({});
		sendData.fakuananswer = JSON.stringify({});
		sendData.low_ansjson = JSON.stringify({});
		sendData.wancheng = "0";
		sendData.pandianid = pandianid;
		showloading();
		$.ajax({  
			url: "../pandianevery/insertPandianJianchaBiaozhun"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify(sendData) 
			,dataType:'json'
			,success: function(res){
				console.log(res);
				try {
					if(res.success){
						var data = res.data;
						if(data.biaozhunid != undefined){
							biaozhunid = data.biaozhunid;
						}
					}else{
						layer.msg(res.message);
					}
				} catch (e) {
					console.log(e)
				} finally{
					hideloading();
				}
				
			}
		});
	}
	if(biaozhunid != ''){
		if($('#biaozhunjiancha').attr('flag') == '1'){
			showFlag = 'update';
//		window.location.href='http://47.92.121.65/jhcvs_xundian/pandian_index/mdwendazonglan.html?dd_nav_bgcolor=FF5E97F6&shopcode='+shopcode+'&shopname='+encodeURI(encodeURI(shopname))+'&dudaoNO='+dudaoNO+'&dudaoName='+encodeURI(encodeURI(dudaoName))+'&every_id='+biaozhunid+'&jieguo=update';
		}
		window.location.href='http://47.92.121.65/jhcvs_xundian/pandian_index/mdwendazonglan.html?dd_nav_bgcolor=FF5E97F6&pandianid='+pandianid+'&fanhui=fanhui&shopcode='+shopcode+'&shopname='+encodeURI(encodeURI(shopname))+'&dudaoNO='+dudaoNO+'&dudaoName='+encodeURI(encodeURI(dudaoName))+'&every_id='+biaozhunid+'&jieguo='+showFlag;
	}
}